package xcmg.device.web.company;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import xcmg.device.infra.AuthCode;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.company.SupplierClassService;
import xcmg.device.service.vo.company.SupplierClassVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;

import java.util.List;

/**
 * Created by sqq on 2017/12/21.
 */

@RestController
@RequestMapping("/supplierClass")
public class SupplierClassController extends BasicController {

    @Autowired
    private SupplierClassService supplierClassService;

    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true,code = AuthCode.Supplier.CLASS_ADD)
    public ActionResult insert(@RequestBody SupplierClassVO supplierClassVO)throws Exception{
        supplierClassVO.setCreateId(getUserId());
        supplierClassVO.setUpdateId(getUserId());
        supplierClassVO.setOrgId(getOrgId());
        ErrorCode code=supplierClassService.insert(supplierClassVO);
        return actionResult(code,supplierClassVO.getId());
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true,code = AuthCode.Supplier.CLASS_EDIT)
    public ActionResult update(@RequestBody SupplierClassVO supplierClassVO){
        if(supplierClassVO.getId()==null||supplierClassVO.getId()<=0){
            return actionResult(ErrorCode.IllegalArument);
        }
        supplierClassVO.getUpdateId();
        ErrorCode code=supplierClassService.update(supplierClassVO);
        return actionResult(code);
    }

    @RequestMapping(value = "/remove",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true,code = AuthCode.Supplier.CLASS_DEL)
    public ActionResult delete(@RequestBody SupplierClassVO supplierClassVO){
        if(supplierClassVO.getId()==null||supplierClassVO.getId()<=0){
            return actionResult(ErrorCode.IllegalArument);
        }
        supplierClassVO.setUpdateId(getUserId());
        ErrorCode code = supplierClassService.delete(supplierClassVO);
        return  actionResult(code);
    }

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult list(@RequestBody SupplierClassVO supplierClassVO)throws Exception{
        supplierClassVO.setOrgId(getOrgId());
        List<SupplierClassVO> results =supplierClassService.list(supplierClassVO);
        return actionValueResult(results);
    }

}
